package com.middle.common.base.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.middle.common.base.mapper.BaseMapper;
import com.middle.common.base.service.BaseService;

public abstract  class BaseServiceImpl<T> implements BaseService<T> {
	
	
	public abstract BaseMapper<T> getDao();

	@Override
	public T getById(Serializable id) {
		return getDao().selectByPrimaryKey(id);
	}

	@Override
	public List<T> getAll() {
		return getDao().selectAll();
	}

	@Override
	public int deleteById(Serializable id) {
		return getDao().deleteByPrimaryKey(id);
	}

	@Override
	public int save(T t) {
		return getDao().save(t);
	}

	@Override
	public int saveBatch(Collection<T> collection) {
		List<T> list = collection.stream().collect(Collectors.toList());
		return getDao().insertList(list);
	}

	@Override
	public int update(T t) {
		return getDao().updateByPrimaryKeySelective(t);
	}

	@Override
	public int updateBatch(Collection<T> collection) {
		int num = 0;
		for(T t : collection) {
			num ++;
			getDao().updateByPrimaryKey(t);
		}
		return num;
	}

}
